import Log from '../../../share/common/util/log.js'
import router from '@system.router';
import app from '@system.app';
import config from '../../../../js/share/common/config/config.js'
import bleOperator from '../../../../js/share/common/opinterface/bleoperator.js'
import commonOperator from '../../../../js/share/common/opinterface/commonoperator.js'
import bleDataUtils from '../../../../js/share/common/util/bleDataUtils.js'
import {showToast} from '../../../share/common/util/util.js'

export default {
    data: {
        macAddr: undefined,
        deviceName: "智慧农业感应板",
        tagId: undefined,
        reportValue1: '--', //土壤湿度
        reportValue2: '--', //环境温度
        reportValue3: '--', //环境湿度
        threshold1: '', //水泵开关阈值
        threshold2: '', //环境温度报警阈值
        threshold3: '', //环境湿度报警阈值
        disPlayFeatures: [
            {
                name: "当前水泵状态",
                value: false,
                icon: "/common/compose_tshuibou.png"
            },
            {
                name: "当前风扇状态",
                value: false,
                icon: "/common/compose_fengsan.png"
            }
        ],
        dataArray1: [],
        dataArray2: [],
        dataArray3: [],
        lineData: undefined, // 线形图数据
        lineOps: { // 线形图样式
            xAxis: {
                min: 0,
                max: 10,
                axisTick: 5,
                display: true,
                color: "#1a000000"
            },
            yAxis: {
                min: 0,
                max: 50,
                axisTick: 5,
                display: true,
                color: "#1a000000"
            },
            series: {
                lineStyle: {
                    width: '1px',
                    smooth: true
                },
                headPoint: {
                    shape: 'circle',
                    size: 2,
                    strokeWidth: 0,
                    fillColor: '#8477DF',
                    strokeColor: '#8477DF',
                    display: true
                },
                loop: {
                    margin: 2,
                }
            }
        },
        isPageShow: false
    },
    onInit() {
        this.isPageShow = true
        if (this.params == undefined) {
            Log.error('Params is undefined!');
        } else {
            let paramJson = JSON.parse(this.params);
            this.tagId = paramJson.tagId;
            this.macAddr = paramJson.macAddr;
            this.threshold1 = paramJson.threshold1;
            this.threshold2 = paramJson.threshold2;
            this.threshold3 = paramJson.threshold3;
            this.reportValue1 = paramJson.reportValue1;
            this.reportValue2 = paramJson.reportValue2;
            this.reportValue3 = paramJson.reportValue3;
        }
        this.disPlayFeatures[0].value = parseInt(this.reportValue1) < parseInt(this.threshold1)
        this.disPlayFeatures[1].value = parseInt(this.reportValue2) > parseInt(this.threshold2)
        this.initListener()
        setTimeout(() => {
            this.changeLineData(0, parseInt(this.reportValue2))
            this.changeLineData(1, parseInt(this.reportValue3))
            this.changeLineData(2, parseInt(this.reportValue1))
        }, 500)
    },
    onShow() {
        this.isPageShow = true
    },
    onHide() {
        this.isPageShow = false
    },
    getLine(color, value) {
        var obj = {
            "value": value, // Y坐标
            "description": value + "", // 当前点的注释内容
            "textLocation": "top", // 注释内容位置
            "textColor": color, // 注释内容颜色
            "pointStyle": { // 节点形状
                "shape": "circle", // 形状
                "size": 2, // 形状大小
                "fillColor": color, // 填充颜色
                "strokeColor": color // 边框颜色
            }
        };
        return obj;
    },

    // 更新线形图数据
    changeLineData(serialPos, value) {
        if (this.dataArray1.length > 10 || this.dataArray2.length > 10 || this.dataArray3.length > 10) {
            this.dataArray1.shift()
            this.dataArray2.shift()
            this.dataArray3.shift()
        }
        if (serialPos == 0) {
            let color = "#4380FA"
            this.dataArray1.push(this.getLine(color, value))
        } else if (serialPos == 1) {
            let color = "#FAA35C"
            this.dataArray2.push(this.getLine(color, value))
        } else if (serialPos == 2) {
            let color = "#7250FA"
            this.dataArray3.push(this.getLine(color, value))
        }
        this.lineData = [
            {
                strokeColor: '#4380FA',
                fillColor: '#334380fa', // 线的颜色
                data: this.dataArray1,
                gradient: true,
            },
            {
                strokeColor: '#FAA35C',
                fillColor: '#33FAA35C', // 线的颜色
                data: this.dataArray2,
                gradient: true,
            },
            {
                strokeColor: '#7250FA',
                fillColor: '#337250FA', // 线的颜色
                data: this.dataArray3,
                gradient: true,
            }
        ]
    },
    //        changeLineData(serialPos, value) {
    //            this.$refs.linechart.append({
    //                serial: serialPos,
    //                //            data: [value],
    //                data: {
    //                    "value": value, // Y坐标
    //                    "description": value + "", // 当前点的注释内容
    //                    "textLocation": "top", // 注释内容位置
    //                    "textColor": "#CDCACA", // 注释内容颜色
    //                    "pointStyle": { // 节点形状
    //                        "shape": "circle", // 形状
    //                        "size": 2, // 形状大小
    //                        "fillColor": "#CF0A2C", // 填充颜色
    //                        "strokeColor": "#CF0A2C" // 边框颜色
    //                    }
    //                }
    //            })
    //        },
    initListener() {
        bleOperator.onBleCharacteristicValueChange(this.onBleCharacteristicValueChange);
    },
    onBleCharacteristicValueChange(callbackJson) {
        if (!this.isPageShow) {
            return
        }
        Log.info("onBleCharacteristicValueChange" + ' value changed: ' + JSON.stringify(callbackJson));
        switch (callbackJson.data.characteristicId) {
            case config.CharacterNotify_UUID:
                this.dataTreating(callbackJson.data.changeData.toUpperCase());
                break;
            default:
                break;
        }
    },
    dataTreating(changeData) {
        if (!bleDataUtils.verifyProtocol(changeData)) {
            return;
        }
        let tagDataArray = bleDataUtils.parseCharacteristic(changeData)
        Log.info("dataTreating" + ' tagDataArray: ' + JSON.stringify(tagDataArray));
        tagDataArray.forEach(element => {
            let tag = element[0]
            let value = parseInt(element[2], 16)
            switch (this.tagId) {
                case '00070009': //双传感器组合
                    if (tag == '0007') {
                        this.reportValue1 = value
                        this.disPlayFeatures[0].value = parseInt(this.reportValue1) < parseInt(this.threshold1)
                        this.changeLineData(2, value)
                    } else if (tag == '0008') {
                        this.threshold1 = value
                    } else if (tag == '0009') {
                        this.reportValue2 = value
                        this.disPlayFeatures[1].value = parseInt(this.reportValue2) >= parseInt(this.threshold2)
                        this.changeLineData(0, value)
                    } else if (tag == '000A') {
                        this.reportValue3 = value
                        this.changeLineData(1, value)
                    } else if (tag == '000B') {
                        this.threshold2 = value
                    } else if (tag == '000C') {
                        this.threshold3 = value
                    }
                    break;
                default:
                    break;
            }
        });
    },
    controlClick(id) {
        switch (id) {
            case 0:

                break;
            case 1:

                break;
            default:
                break

        }
    },
    //下发阀值数据
    writeBleData(data) {
        Log.info("writeBleData value = " + data);
        bleOperator.writeBleCharacteristicValue(this.macAddr, config.Service_UUID, config.CharacterWrite_UUID, data, callbackJson => {
            Log.info("writeBleCharacteristicValue : " + JSON.stringify(callbackJson.data.writeData));
            showToast("阀值设置成功!")
        })
    },
    change1(e) {
        this.threshold1 = e.value
    },
    enterkeyClick1(e) {
        //        showToast("水泵开关阈值value: " + this.threshold1)
        let data = bleDataUtils.getTagsCommandHexStr("0008", config.MType[0], parseInt(this.threshold1))
        this.writeBleData(data)
    },
    change2(e) {
        this.threshold2 = e.value
    },
    enterkeyClick2(e) {
        //        showToast("温度报警阈值value: " + this.threshold2)
        let data = bleDataUtils.getTagsCommandHexStr("000B", config.MType[0], parseInt(this.threshold2))
        this.writeBleData(data)
    },
    change3(e) {
        this.threshold3 = e.value
    },
    enterkeyClick3(e) {
        //        showToast("湿度报警阈值value: " + this.threshold3)
        let data = bleDataUtils.getTagsCommandHexStr("000C", config.MType[0], parseInt(this.threshold3))
        this.writeBleData(data)
    },
    backClicked() {
        router.back()
    },
}
